package com.winit.deploy.web;

import com.alibaba.fastjson.JSONObject;
import com.winit.common.base.beans.LoginUser;
import com.winit.common.base.web.BaseController;
import com.winit.common.filter.UserInfoUtil;
import com.winit.common.util.LogUtil;
import com.winit.deploy.manager.DeployAuthorizeManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 发布授权
 */
@RestController
@RequestMapping("/deploy")
public class DeployAuthorizeController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(DeployAuthorizeController.class);

    @Resource
    private DeployAuthorizeManager authorizeManager;

    /**
     * 获取授权信息
     *
     * @param request
     * @return
     */
    @RequestMapping("/getAuthorize")
    @ResponseBody
    public Map<String, Object> getAuthorize(HttpServletRequest request) {
        Map<String, Object> rtnMap = new HashMap<>();
        LoginUser userInfo = UserInfoUtil.getUserInfo(request);
        if (userInfo == null) {
            logger.info("获取授权信息,获取用户信息为空");
            return this.noUserRtn();
        }
        JSONObject object = authorizeManager.getAuthorize();
        return this.getRtnMap(rtnMap, userInfo, object);
    }

    /**
     * 授权 取消授权
     *
     * @param request
     * @return
     */
    @RequestMapping("/doAuthorize")
    @ResponseBody
    public Map<String, Object> doAuthorize(HttpServletRequest request) {
        Map<String, Object> rtnMap = new HashMap<>();
        LoginUser userInfo = UserInfoUtil.getUserInfo(request);
        if (userInfo == null) {
            logger.info("授权 取消授权,获取用户信息为空");
            return this.noUserRtn();
        }

        String type = request.getParameter("type");//0 授权  1 取消授权
        String hours = request.getParameter("hours");//授权时长

        try {
            if ("0".equals(type)) {
                if (StringUtils.isEmpty(hours)) {
                    logger.info("com.winit.deploy.web.DeployAuthorizeController.doAuthorize hours为空");
                    rtnMap.put("code", "1");
                    rtnMap.put("desc", "参数hours为空");
                } else {
                    LogUtil.operateLog(userInfo, "", "", "online", LogUtil.OperateType.AUTHORIZE, hours, "");
                    authorizeManager.doAuthorize(hours);
                    rtnMap.put("code", "0");
                }
            } else if ("1".equals(type)) {
                LogUtil.operateLog(userInfo, "", "", "online", LogUtil.OperateType.CANCEL_AUTHORIZE, "", "");
                authorizeManager.cancelAuthorize();
                rtnMap.put("code", "0");
            } else {
                logger.info("com.winit.deploy.web.DeployAuthorizeController.doAuthorize type错误:" + type);
                rtnMap.put("code", "1");
                rtnMap.put("desc", "参数type错误." + type);
            }
        } catch (Exception e) {
            rtnMap.put("code", "1");
            rtnMap.put("desc", e.getLocalizedMessage());
        }
        JSONObject object = authorizeManager.getAuthorize();
        return this.getRtnMap(rtnMap, userInfo, object);
    }

    /**
     * 终止发布
     * @param request
     * @return
     */
    @RequestMapping("/stopDeploy")
    @ResponseBody
    public Map<String, Object> stopDeploy(HttpServletRequest request) {
        Map<String, Object> rtnMap = new HashMap<>();
        LoginUser userInfo = UserInfoUtil.getUserInfo(request);
        if (userInfo == null) {
            logger.info("终止发布,获取用户信息为空");
            return this.noUserRtn();
        }

        String envName = request.getParameter("envName");
        Assert.hasText(envName);
        authorizeManager.updateStopDeployTip(envName,true);
        rtnMap.put("code","0");
        return this.getRtnMap(rtnMap, userInfo, new JSONObject());
    }
}
